//package com.tzy.evaluation.service;
//import com.tzy.common.constant.CacheConstant;
//import com.tzy.common.utils.FastjsonUtil;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Service;
//
//import javax.annotation.Resource;
//import java.time.LocalDateTime;
//import java.utils.*;
//
//@Service
//@Slf4j
//public class LikedService /*implements ILikedService*/ {
//////    @Resource
//////    UserLikeRepository likeRepository;
//////    @Resource
//////    RedisServicePlus redisServicePlus;
//////    @Resource
//////    IUserService userService;
//    @Resource
//    private UserLikesService userLikesService;
//    @Resource
//    RedisTemplate redisTemplate;
////
////    /**
////     * //参数验证
////     */
//////    boolean validateParam(int articleId,int  likedUserId,int  likingUserId){
//////        return like
//////    }
//    /**
//     * 点赞
//     *
//     * @param articleId
//     * @param likedUserId
//     * @param likingUserId
//     */
//    public void likeEvaluation(Long articleId, Long likedUserId, Long likingUserId) {
//
//
//        log.info("点赞数据存入redis开始，articleId:{}，likedUserId:{}，likingUserId:{}", articleId, likedUserId, likingUserId);
//        synchronized (this) {
//            /**
//             * 只有未点赞的用户才可以进行点赞
//             */
////            likeArticleLogicValidate(articleId, likedUserId, likingUserId);
//            //1.用户总点赞数+1
//            redisTemplate.opsForHash().increment(CacheConstant.EVALUATION_TOTAL_LIKED_COUNT, String.valueOf(likedUserId), 1);
//
//            //2.用户喜欢的文章+1
//            String userLikeResult = (String) redisTemplate.opsForHash().get(CacheConstant.LIKING_USER_ID, String.valueOf(likingUserId));
//            List<Long> articleIdList = new ArrayList<>();
////            userLikeResult == null ? new ArrayList<Long>() : FastjsonUtil.toList(userLikeResult, Long.class);
//            articleIdList.add(articleId);
//            redisTemplate.opsForHash().put(CacheConstant.LIKING_USER_ID, String.valueOf(likingUserId), FastjsonUtil.json2List(articleIdList));
//
//            //3.文章点赞数+1
//            String articleLikedResult = (String) redisTemplate.opsForHash().get(CacheConstant.EVALUATION_LIKED_USER, String.valueOf(articleId));
////            List<Long> likePostIdSet = articleLikedResult == null ? new HashSet<Long>() : FastjsonUtil.toList(articleLikedResult, Long.class);
//            List<Long> likePostIdSet = articleLikedResult == null ? new ArrayList<>() : FastjsonUtil.toList(articleLikedResult, Long.class);
//            likePostIdSet.add(likingUserId);
//            redisTemplate.opsForHash().put(CacheConstant.EVALUATION_LIKED_USER, String.valueOf(articleId), FastjsonUtil.convertObjectToJSON(likePostIdSet));
//            log.info("取消点赞数据存入redis结束，articleId:{}，likedUserId:{}，likingUserId:{}", articleId, likedUserId, likingUserId);
//        }
//    }
//
////    private void validateParam(Long articleId, Long likedUserId, Long likingUserId) {
////
////    }
////
////    @Scheduled(cron = "0 0 0/1 * * ? ")
////    public void redisDataToMySQL() {
////        log.info("time:{}，开始执行Redis数据持久化到MySQL任务", LocalDateTime.now().format(formatter));
////        //1.更新文章总的点赞数
////        Map<String, String> articleCountMap = redisTemplate.opsForHash().entries(CacheConstant.EVALUATION_LIKED_USER);
////        for (Map.Entry<String, String> entry : articleCountMap.entrySet()) {
////            String articleId = entry.getKey();
////            Set<Long> userIdSet = FastjsonUtil.deserializeToSet(entry.getValue(), Long.class);
////            //1.同步某篇文章总的点赞数到MySQL
////            synchronizeTotalLikeCount(articleId, userIdSet);
////            //2.同步用户喜欢的文章
////            synchronizeUserLikeArticle(articleId, userIdSet);
////        }
////        log.info("time:{}，结束执行Redis数据持久化到MySQL任务", LocalDateTime.now().format(formatter));
////    }
////
////
////////    @Override
//////    @Transactional
//////    public UserLike save(UserLike userLike) {
//////        return likeRepository.save(userLike);
//////    }
//////
////////    @Override
//////    @Transactional
//////    public List<UserLike> saveAll(List<UserLike> list) {
//////        return likeRepository.saveAll(list);
//////    }
//////
////////    @Override
////////    public Page<UserLike> getLikedListByLikedUserId(String likedUserId, Pageable pageable) {
////////        return likeRepository.findByLikedUserIdAndStatus(likedUserId, LikedStatusEnum.LIKE.getCode(), pageable);
////////    }
////////
////////    @Override
////////    public Page<UserLike> getLikedListByLikedPostId(String likingUserId, Pageable pageable) {
////////        return likeRepository.findByLikedPostIdAndStatus(likingUserId, LikedStatusEnum.LIKE.getCode(), pageable);
////////    }
//////
////////    @Override
//////    public UserLike getByLikedUserIdAndLikedPostId(String likedUserId, String likingUserId) {
//////        return likeRepository.findByLikedUserIdAndLikedPostId(likedUserId, likingUserId);
//////    }
//////
////////    @Override
//////    @Transactional
//////    public void transLikedFromRedis2DB() {
//////        List<UserLike> list = redisServicePlus.getLikedDataFromRedis();
//////        for (UserLike like : list) {
//////            UserLike ul = getByLikedUserIdAndLikedPostId(like.getLikedUserId(), like.getLikingUserId());
//////            if (ul == null) {
//////                //没有记录，直接存入save(like);
//////            } else {
//////                //有记录，需要更新
//////                ul.setStatus(like.getStatus());
//////                save(ul);
//////            }
//////        }
//////    }
////
//////    @Override
//////    @Transactional
//////    public void transLikedCountFromRedis2DB() {
//////        List<LikedCountDTO> list = redisServicePlus.getLikedCountFromRedis();
//////        for (LikedCountDTO dto : list) {
//////            User user = userService.getById(dto.getId());
//////            //点赞数量属于无关紧要的操作，出错无需抛异常if (user != null){
//////            Integer likeNum = user.getLikeNum() + dto.getCount();
//////            user.setLikeNum(likeNum);
//////            //更新点赞数量
//////            userService.updateById(user);
//////        }
//////    }
//}